<?php

namespace MarketServer\Model;

/**
 * Description of NoticesModel
 *
 * @since 1.0 <2015-8-7> SoChishun <14507247@qq.com> Added.
 */
class NoticesModel extends AppbaseModel {

    protected $tableName = 'tarticle';
// 命名范围
    protected $_scope = array(
        'export' => array(
            'field' => "site_id as '租户ID', user_name as '用户名'",
            'order' => 'site_id',
            'cache' => true,
        ),
        'item_list' => array(
            'field' => 'id, title, category_id, image_url, visit_count, state, update_time, create_time',
            'order' => 'id desc',
            'cache' => true,
        ),
    );
    
    protected $patchValidate = true;

    /**
     * 保存公告
     * @param string $admin_name 操作员名称
     * @return array
     * @since 1.0 2015-8-7 by sutroon
     */
    public function save_notices($admin_name) {
        $validator = array(
            array('title', 'require', '标题无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('content', 'require', '内容无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('state', 'number', '状态有误!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        $is_edit = I('id') ? true : false;
        if (!$this->validate($validator)->create()) {
            D('Log')->log_user_operate(false, $admin_name, array(
                'admin_name' => $admin_name,
                'desc' => sprintf('%s公告失败 user=%s %s', ($is_edit ? '编辑' : '新增'), $admin_name, implode('; ', $this->getError())),
                'action' => ($is_edit ? LogModel::ACTION_EDIT : LogModel::ACTION_ADD),
                'level' => LogModel::LEVEL_INFO,
            ));
            return $this->returnMsg(false, $this->getError());
        }
        $data = $this->data;
        if ($is_edit) {
            $this->data['update_time'] = date('Y-m-d H:i:s'); // 更新编辑时间
            $result = $this->save();
        } else {
            $this->data['ex_tags'] = ArticleModel::EX_NOTICES;
            $result = $this->add();
        }
        D('Log')->log_user_operate($result, $admin_name, array(
            'admin_name' => $admin_name,
            'desc' => sprintf('%s公告%s user=%s', ($is_edit ? '编辑' : '新增'), '{%}', $admin_name),
            'action' => ($is_edit ? LogModel::ACTION_EDIT : LogModel::ACTION_ADD),
            'level' => LogModel::LEVEL_INFO,
        ));
        return $this->returnMsg($result);
    }

    /**
     * 删除公告
     * @param string $id 主键编号,多个之间以逗号隔开
     * @param int $admin_name 操作员编号
     * @return array
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function notices_delete($id, $admin_name) {
        if (!$admin_name) {
            return $this->returnMsg(false, '管理员无效');
        }
        if (!$id) {
            return $this->returnMsg(false, '编号无效');
        }
        $result = $this->delete($id);
        D('Log')->log_user_operate($result, $admin_name, array(
            'admin_name' => $admin_name,
            'desc' => "删除公告{%}($id) ,操作员:$admin_name",
            'action' => LogModel::ACTION_DELETE,
            'level' => LogModel::LEVEL_INFO,
        ));
        return $this->returnMsg($result);
    }

    /**
     * 返回列表
     * @return array
     * @xince 1.0 <2015-8-7> SoChishun Added.
     */
    public function get_notices_list($where) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count('id');
        $list = $this->cache(true)->scope('item_list')->where($where)->order($orderby)->page($page)->select();
        foreach ($list as $row) {
            $row['state_text'] = sofn_get_state_text('display-hide', $row['state']);
            $out[] = $row;
        }
        return array('total' => $total, 'rows' => $out);
    }

}
